// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false;


// 全局函数
import Global from './components/Global/Global.js'
import Filter from './components/Global/Filter.js'
Vue.use(Global);
Vue.use(Filter);


// axios
import axios from 'axios';
Vue.prototype.$http = axios;
axios.defaults.baseURL = '';
axios.defaults.timeout = 1000;
//默认的contenttype为json以及utf-8；
// axios.defaults.headers = {'Content-Type': 'text/json;charset=utf8'};
axios.defaults.headers = {'Content-Type': 'application/x-www-form-urlencoded'};

import qs from 'qs';
Vue.prototype.$qs = qs;


// 全局路由跳转拦截
sessionStorage.setItem("token", "");
router.beforeEach(function (to, from, next) {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    let token = Vue.prototype.getCookie("token");
    if (token) {
      if (to.path === '/Login') {
        next({
            path: '/',
          }
        )
      }
      else{
        next();
      }
    }
    else {
      if (to.path === '/Login') {
        next()
      }
      else {
        next({
          path: '/Login',
          query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
      }
    }
  }
  else {
    next();
  }
});


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: {App},
  template: '<App/>'
});
